package com.tianqu.tms.core.taglib;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;

import com.tianqu.tms.core.util.TmsConstants;

/**
 * 导航用的class
 * @author liugang
 *
 */
public class NavigationTag extends MenuTagSupport{
    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String HTML_BEGIN= "<div class=\"right-main-menu\"> <p id=\"leftCss\">";
	private static final String HTML_END= "</p>";
    private static final String LINKSPACE = "&nbsp;&gt;&nbsp";
    
    
    /**
     * 右边的链接参数
     */
    private static final String HTML_RIGHT_BEGIN = "<div class=\"tms-right-main-menuR\" id=\"menuR\">";
    private static final String HTML_RIGHT_END = "</div>";
    private static final String HTML_SLINE = "&nbsp;||&nbsp;";//"||"
    
    
	private String funcId;
	//menu配置文件所生成的对象
	private MenuBean rootMenuVO = null;
	/**
	 * 
	 */
	public NavigationTag() {
	}

	/**
	 * 标签开始
	 */
	public int doStartTag() throws JspException {
		MenuBean targetVO = new MenuBean();
		// zhk liu.xingya start
		//menu对象获取
//		rootMenuVO = (MenuBean) pageContext.getServletContext().getAttribute(
//				TmsConstants.MENU_VO_KEY);
		rootMenuVO = (MenuBean) pageContext.getRequest().getAttribute(TmsConstants.MENU_VO_KEY);
		if (rootMenuVO != null) {
			funcId = pageContext.getRequest().getParameter("funcId");
		} else {
			rootMenuVO = (MenuBean) pageContext.getServletContext().getAttribute(
					TmsConstants.MENU_VO_KEY);
		}
		// zhk liu.xingya end
		
		//funcId不存在
		if (funcId == null) {
			throw new JspTagException("funcId is required");
		}
		//menu对象不存在
		if (rootMenuVO == null) {
			throw new JspTagException("Cannot find MenuBean:000");
		}
		//指定的funcId在menu配置文件不存在
		targetVO = rootMenuVO.getMenuVO(funcId);
		if (targetVO == null) {
			throw new JspTagException("Cannot find MenuBean:"+funcId);
		}
		//生成导航menu
		writeAcross(targetVO);
		return EVAL_BODY_INCLUDE;
	}

	/**
	 * 
	 * @param targetVO
	 * @throws JspException
	 */
	private void writeAcross(MenuBean targetVO) throws JspException { 
		/*
		* <div class="right-main-menu">
    	* <p>
        * 	<a href="">中天检测系统</a>&nbsp;&gt;&nbsp;
        * </p>
        * </div>
        */
		StringBuffer sb = new StringBuffer(); 
		MenuBean currentVO = targetVO;
		while(currentVO != null){
			String link = makeLink(currentVO);
		    currentVO = currentVO.getParent();
		    if(currentVO != null) {
		    	link = LINKSPACE.concat(link);
		    }
		    sb.insert(0,link);
		}
		sb.insert(0,HTML_BEGIN);
		
		sb.append(HTML_END);
		sb.append(HTML_RIGHT_BEGIN);
		if(targetVO.getParent()!=null)
		{ 
			Iterator iterator = targetVO.getParent().getChildrenList().iterator();
			while (iterator.hasNext()) {
				MenuBean vo = (MenuBean) iterator.next();
				if(vo.getLevel() == 2 && !vo.getMenuName().equals(targetVO.getMenuName()))
				{
					if(hasViewCompetence(vo)){
					sb.append(HTML_SLINE);
					sb.append("<a href=\"");
					sb.append(vo.getTargetUri());
					sb.append("\">");
					sb.append(vo.getMenuName());
					sb.append("</a>");
					}
				}
			}	
			if(targetVO.getLevel() == 2)
			{
				String str = targetVO.getFunctionId().substring(0, 1)+"000";
				sb.append(HTML_SLINE.concat("<a href=\"menu.tq?funcId="+str+"\">返回&nbsp;&gt;&gt;</a>"));
			}
		}
		sb.append(HTML_RIGHT_END);
		sb.append(HTML_RIGHT_END);
		try {
			JspWriter out = pageContext.getOut(); 
			out.print(sb.toString());
		}catch(IOException e){
			throw new JspException("across:" + e.toString());
		}
	}
	/**
	 * 生成<a>标签
	 * @param nb
	 * @return String
	 */
	private String makeLink(MenuBean nb){
	    StringBuffer sb = new StringBuffer();
	    if(nb != null){
//		    sb.append("<a href=\"");
//			sb.append(nb.getTargetUri());
//			sb.append("\">");
			sb.append(nb.getMenuName());
//			sb.append("</a>");
	    }
		return sb.toString();
	}
	
	public void setFuncId(String funcIdP) {
		this.funcId = funcIdP;
	}
	public String getFuncId() {
		return this.funcId;
	}
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

}
